Off-By-One Woes#

In the first post on FFTs, there was a remark about not being able to tell exactly what frequency the main peak occurred at, because when it falls between frequency points it’s hard to be sure.

Looking into this more closely, I found that an off-by-one error had crept into the FFT output at some time in the past. GF4 uses the rfft routine from Numpy, and the first point represents DC: a frequency of zero. The formula for computing the frequency step size was also simplified.

Having fixed that, a close look at the sine and damped sine routines also led to a slight adjustment. The last point of the sine wave should have a value just one short of zero: if the curve were to be extended by one more point, it should start the next cycle and so have the value zero. There was a slight discrepancy, which is now fixed.

With these two fixes, a sine wave having exactly five cycles across the span of the data has an FFT whose peak falls exactly on the 5th frequency step, as it should.

These fixes are in the devel and master branches after May 10.